//
//  SpecialTerm.swift
//  HealthMonitoring
//
//  Created by YTYanK on 2018/8/4.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit

class SpecialTerm: HMViewController {
    let position = UILabel()
    let img = UIImageView()
    let bg = UIImageView()
    let submit = UIButton()
    var stData = Array<Any>()
    var stID = String() // 父级的ID
    /// 选择中的 id
    var selectSubIDs = Array<Int>()
    var btnData: [String]? = ["头部", "身体", "身体2", "身体2", "四肢"]
    var btnLayout: [(String,String,String,String,Bool)]? = [("175","20","0","0", false),("300","15","0","0", false),("390","20","0","0", false),("310","-20","0","0", false),("390","-20","0","0", false)]
    var btnAry: [UIButton]?
    var titleAry: [String] = []
    
    
    override func setupView() {
        self.title = "专项报告"
        view.backgroundColor = UIColor.white
        
        
        view.addSubview(bg)
        view.addSubview(img)
        view.addSubview(position)
        view.addSubview(submit)
        
        
        //        view.addSubview(btn1)
        //        view.addSubview(btn2)
        //        view.addSubview(btn3)
        //        view.addSubview(btn4)
        
        bg.topAlign(view: view, predicate: "0")
        bg.leadingAlign(view: view, predicate: "0")
        bg.trailingAlign(view: view, predicate: "0")
        bg.bottomAlign(view: view, predicate: "0")
        bg.image = UIImage(named: "presentationBg.png")
        
  
        if self._signStr == "头部" {
            img.topAlign(view: view, predicate: "90")
            img.leadingAlign(view: view, predicate: "50")
            img.trailingAlign(view: view,predicate: "-50")
            img.bottomAlign(view: view, predicate: "-250")
            img.imgName(name: "amanhead.png")
            
        }else if self._signStr == "脊椎" {
            img.topAlign(view: view, predicate: "100")
            img.leadingAlign(view: view, predicate: "0")
            img.trailingAlign(view: view,predicate: "0")
            img.bottomAlign(view: view, predicate: "-220")
            img.imgName(name: "jz.png")
            
        }else if self._signStr == "身体/体液"{
            img.topAlign(view: view, predicate: "100")
            img.leadingAlign(view: view, predicate: "0")
            img.trailingAlign(view: view,predicate: "0")
            img.bottomAlign(view: view, predicate: "-220")
            img.imgName(name: "st.png")
        }else {
            img.topAlign(view: view, predicate: "100")
            img.leadingAlign(view: view, predicate: "0")
            img.trailingAlign(view: view,predicate: "0")
            img.bottomAlign(view: view, predicate: "-220")
            img.imgName(name: "sz.png")
        }
        
        position.topAlign(view: view,predicate: "0")
        position.leadingAlign(view: view, predicate: "0")
        position.trailingAlign(view: view,predicate: "0")
        position.heightConstrain(predicate: "44")
        
        position.text = "可以多选!"
        position.textAlignment = NSTextAlignment.center
        position.backgroundColor = UIColor.gray
        position.textColor = UIColor.white
        
        submit.bottomAlign(view: view, predicate: "-50")
        submit.centerXAlign(view: view)
        submit.leadingAlign(view: view, predicate: "20")
        submit.trailingAlign(view: view, predicate: "-20")
        submit.setTitle("提交报告", for: .normal)
        submit.heightConstrain(predicate: "44")
        submit.addTarget(self, action: #selector(addPresentation), for: .touchUpInside)
        submit.backgroundColor = configure.shareConfigure.HMColor1
        
        setButtonSytle()
        
        var i = 0
        for v in stData { //btnData! {
            
            let dic = v as! Dictionary<String, Any>
            let btn = UIButton()
            btn.tag = dic["sid"] as! Int
            
            btnAry?.append(btn)
            view.addSubview(btn)
            
          //  let str = btnLayout![i].0
            btn.topAlign(view: view, predicate: btnLayout![i].0)
            if btnLayout![i].1.contains("-") {
                btn.trailingAlign(view: view, predicate:  btnLayout![i].1)
            }else {
                btn.leadingAlign(view: view, predicate:  btnLayout![i].1)
            }
            btn.widthConstrain(predicate: "80")
            btn.heightConstrain(predicate: "30")
            btn.isSelected =  btnLayout![i].4
            if btnLayout![i].4 == true {
                btn.backgroundColor = configure.shareConfigure.HMColor1
            }else {
               btn.backgroundColor = configure.shareConfigure.HMColor3
            }
            
            btn.layer.cornerRadius = 14
            btn.setTitle((dic["name"] as! String), for: .normal)
            btn.setTitleColor(UIColor.white, for: .normal)
            btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
            i += 1
        }
        
        
    }
    
    //
    @objc func addPresentation(sender: UIButton) {
        let er = HMEntryReport()
        er.erType = 2
        er.pId = self.stID
        er.subIds = self.selectSubIDs
        self.pushViewController(er)
        
    }
    // 选择
    @objc func btnClick(sender: UIButton) {
        
        sender.isSelected = !sender.isSelected
        if sender.isSelected == true {
            sender.backgroundColor = configure.shareConfigure.HMColor1
            self.titleAry.append((sender.titleLabel?.text)!)
            self.selectSubIDs.append(sender.tag)
            
        }else {
            sender.backgroundColor = configure.shareConfigure.HMColor3
            
            let index = self.titleAry.index(of: (sender.titleLabel?.text)!)
            self.titleAry.remove(at: index!)
            self.selectSubIDs.remove(at: index!)
        }
        print("---->>\(self.titleAry)")
        
        setButtonSytle()
    }

    
    // 设置按钮表现效果
    func setButtonSytle()  {
        //判断是否开启按钮
        if (self.selectSubIDs.count > 0) {
            openButtonRecognition(setStyleBtn: self.submit, isOpen: true)
        }else {
            openButtonRecognition(setStyleBtn: self.submit, isOpen: false)
        }
    }
}

